﻿@model OrderModel

<table class="adminContent">
    <tr>
        <td>
            <div id="ordernotes-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#ordernotes-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("OrderNotesSelect", "Order", new { orderId = Model.Id }))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                destroy: {
                                    url: "@Html.Raw(Url.Action("OrderNoteDelete", "Order", new { orderId = Model.Id }))",
                                    type: "POST",
                                    dataType: "json"
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors",
                                model: {
                                    id: "Id",
                                    fields: {
                                        //do not implicitly specify all fields
                                        //we do it only for fields which implicitly require it
                                        //otherwise, they'll be formatted wrong way
                                        CreatedOn: {type: "date" }
                                    }
                                }
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            numeric: false,
                            previousNext: false,
                            info: false
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [{
                            field: "CreatedOn",
                            title: "@T("Admin.Orders.OrderNotes.Fields.CreatedOn")",
                            width: 200,
                            type: "date",
                            format: "{0:G}"
                        }, {
                            field: "Note",
                            title: "@T("Admin.Orders.OrderNotes.Fields.Note")",
                            encoded: false
                        },{
                            field: "DownloadId",
                            title: "@T("Admin.Orders.OrderNotes.Fields.Download")",
                            template: '# if(DownloadId > 0) {# <a href="@Url.Content("~/Admin/Download/DownloadFile")?downloadGuid=#=DownloadGuid#">@T("Admin.Orders.OrderNotes.Fields.Download.Link")</a> #} else {# @T("Admin.Orders.OrderNotes.Fields.Download.Link.No") #} #'
                        },{
                            field: "DisplayToCustomer",
                            title: "@T("Admin.Orders.OrderNotes.Fields.DisplayToCustomer")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=DisplayToCustomer#.gif" />'
                        },  {
                            command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                            title: "@T("Admin.Common.Delete")",
                            width: 100
                        }]
                    });
                });
            </script>
        </td>
    </tr>
</table>
<p>
    <strong>
        @T("Admin.Orders.OrderNotes.AddTitle")
    </strong>
</p>


<script type="text/javascript">
    $(document).ready(function () {

        $("#@Html.FieldIdFor(model => model.AddOrderNoteHasDownload)").change(toggleAddOrderNoteHasDownload);

        toggleAddOrderNoteHasDownload();
    });

    function toggleAddOrderNoteHasDownload() {
        if ($('#@Html.FieldIdFor(model => model.AddOrderNoteHasDownload)').is(':checked')) {
            $('#pnlAddOrderNoteDownloadId').show();
        }
        else {
            $('#pnlAddOrderNoteDownloadId').hide();
        }
    }

</script>

<table class="adminContent">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.AddOrderNoteMessage):
        </td>
        <td class="adminData">
            @Html.TextAreaFor(model => model.AddOrderNoteMessage, new { style = "Width: 350px; Height: 150px;" })
            @Html.ValidationMessageFor(model => model.AddOrderNoteMessage)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.AddOrderNoteDownloadId):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.AddOrderNoteHasDownload) @T("Admin.Orders.OrderNotes.Fields.Download.HasDownload")
            <div id="pnlAddOrderNoteDownloadId">
                @Html.EditorFor(model => model.AddOrderNoteDownloadId)
                @Html.ValidationMessageFor(model => model.AddOrderNoteDownloadId)
            </div>
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.AddOrderNoteDisplayToCustomer):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.AddOrderNoteDisplayToCustomer)
            @Html.ValidationMessageFor(model => model.AddOrderNoteDisplayToCustomer)
        </td>
    </tr>
    <tr>
        <td colspan="2" align="left">
            <input type="button" id="addOrderNote" class="k-button" value="@T("Admin.Orders.OrderNotes.AddButton")" />
        </td>
    </tr>
</table>

<script type="text/javascript">
    $(document).ready(function () {
        $('#addOrderNote').click(function () {
            var orderNoteMessage = $("#@Html.FieldIdFor(model => model.AddOrderNoteMessage)").val();
            var orderNoteDownloadId = 0;
            if ($('#@Html.FieldIdFor(model => model.AddOrderNoteHasDownload)').is(':checked')) {
                orderNoteDownloadId = $("#@Html.FieldIdFor(model => model.AddOrderNoteDownloadId)").val();
            }
            var orderNoteDisplayToCustomer = $("#@Html.FieldIdFor(model => model.AddOrderNoteDisplayToCustomer)").is(':checked');
            $('#addOrderNote').attr('disabled', true);
            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("OrderNoteAdd", "Order"))",
                data: { "DisplayToCustomer": orderNoteDisplayToCustomer, "message": orderNoteMessage, "downloadId": orderNoteDownloadId, "orderId": @Model.Id },
                success: function (data) {
                    var ordernotesGrid = $("#ordernotes-grid").data('kendoGrid');
                    //grid.dataSource.page(1); //new search. Set page size to 1
                    ordernotesGrid.dataSource.read();
                    $('#addOrderNote').attr('disabled', false);
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('Failed to add order note.');
                    $('#addOrderNote').attr('disabled', false);
                }  
            });
        });
    });
</script>
